//
//  HomePaternityViewController.m
//  WheatPlan
//
//  Created by 郑渊文 on 6/29/15.
//  Copyright (c) 2015 IOSTeam. All rights reserved.
//

#import "HomePaternityViewController.h"
#import "SameCityCell.h"
#import "MyActivityDetailViewController.h"
#import "EventViewController.h"

@interface HomePaternityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,copy)UITableView *myTable;
@property(nonatomic,copy)NSMutableArray  *activityNameArray;
@property(nonatomic,copy)NSMutableArray  *activityStartTimeArray;
@property(nonatomic,copy)NSMutableArray  *activityEndTimeArray;
@property(nonatomic,copy)NSMutableArray  *activityIdArray;

@end

@implementation HomePaternityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    self.view.backgroundColor = [UIColor whiteColor];
    _activityNameArray = [NSMutableArray array];
    _activityStartTimeArray = [NSMutableArray array];
    _activityEndTimeArray = [NSMutableArray array];
    _activityIdArray = [NSMutableArray array];
    
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *ID = [user objectForKey:@"ID"];

     NSDictionary *param4 = @{@"userId":ID,@"typeId":@"1"};
    [UserDAL getHomePaternityActivityParams:param4 withBlock:^(NSDictionary *data, NSError *error) {
        if (data) {
            //NSLog(@"what%@",data);
            NSArray *array = data[@"dataList"];
            if (array.count !=0) {
                for (NSDictionary *myDic in array) {
                    [_activityNameArray addObject:myDic[@"activityName"]];
                    [_activityStartTimeArray addObject:myDic[@"activityStartTime"]];
                    [_activityEndTimeArray  addObject:myDic[@"activityEndTime"]];
                    [_activityIdArray  addObject:myDic[@"activityId"]];
                }

            } else {
                alert(@"对不起，你所在的地区没有麦苗公学。");
            }
            ////NSLog(@"name中得数据为%@",self.activityNameArray);
        
            [_myTable reloadData];
    }else
        {
           
            alert(@"对不起，你所在的地区没有麦苗公学。");
            
        }
    }];
    // Do any additional setup after loading the view.
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{
    NSString *msg = [[NSString alloc] initWithFormat:@"您点击的是第%d个按钮!",buttonIndex];
    ////NSLog(@"msg:%@",msg);
    if (buttonIndex == 0) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
        ////NSLog(@"1111111");
        
    }else if(buttonIndex == 1){
        
        
        ////NSLog(@"2222222");
        
        
    }
    
    
}

-(void)initSubViews
{
    self.navigationItem.title = @"同城亲子";
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明
    [self.navigationController.navigationBar setBarTintColor:[GetFileModel colorWithHexString:@"#34bd81" alpha:1]];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    
    self.view.backgroundColor = [UIColor clearColor];
    
    
    //自定义左右按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [button setBackgroundImage:[UIImage imageNamed:@"_0000_返回.png"]
     
                      forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(back)
     
     forControlEvents:UIControlEventTouchUpInside];
    
    button.frame = CGRectMake(0, 0, 20, 15);
    
    
    
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    
    
    self.navigationItem.leftBarButtonItem = menuButton;
    
    
    _myTable = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    _myTable.delegate = self;
    _myTable.dataSource = self;
    [self.view addSubview:_myTable];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _activityNameArray.count;
}


#pragma mark--- tableViewDelegate 区尾的高度

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 1;
    
}
#pragma mark--- tableViewDelegate 每行的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 60;
    
}
#pragma mark--- tableViewDelegate cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *NOTIFY = @"cell";
    SameCityCell *cell = (SameCityCell *)[tableView dequeueReusableCellWithIdentifier:NOTIFY];
    if (cell==nil) {
        cell=[[SameCityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NOTIFY];
        
    }
    cell.placeLab.text= _activityNameArray[indexPath.row];
    cell.placeLab.adjustsFontSizeToFitWidth = YES;
    ////NSLog(@"是个什么鬼啊%@",cell.placeLab.text);
    cell.startTime.text = _activityStartTimeArray[indexPath.row];
    cell.endTime.text = _activityEndTimeArray[indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    MyActivityDetailViewController *sugVC = [[MyActivityDetailViewController alloc]init];
//    sugVC.typeId = @"1";
//    sugVC.activityId = _activityIdArray[indexPath.row];
//    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
//    [self  presentViewController:nav animated:YES completion:nil];
    
    
//    MessagerDetailViewController *sugVC = [[MessagerDetailViewController alloc]init];
//    sugVC.messageId = _messageIdArray[indexPath.row];
//    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC];
//    [self  presentViewController:nav animated:YES completion:nil];
    
    
    EventViewController *sugVC1 = [[EventViewController alloc]init];
    sugVC1.activityId = _activityIdArray[indexPath.row];
    sugVC1.typeId = @"1";
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:sugVC1];
    [self  presentViewController:nav animated:YES completion:nil];
    

    
    
}

-(void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

@end
